import { DMessageClient } from '@/util/msgjs/DMessageClient.js'
import { location } from "@/util/tool.js"
const state = {
    socketClient: ""
}

const mutations = {
    setSocketClient: (state, data) => {
        state.socketClient = data
    }
}

const actions = {
    async socketClientInit({ commit }, payload) {
        if (!window.SharedWorker) {
            throw new Error('当前浏览器不支持SharedWorker')
        }
        let hPath = location(window) || 'https://www.codelover.club'
        let subPath = ''
        // debugger
        if (process.env.NODE_ENV === 'production') {
            hPath = location(window) || 'https://www.codelover.club'
            subPath = '/mysocket'
        }
        const client = new DMessageClient(hPath, subPath, payload.handler, payload.roomName);
        // const client = new DMessageClient('http://test.codelover.club', '/mysocket', payload.handler,payload.roomName);
        //  const client = getMessageClient(msgObj, handler)
        commit('setSocketClient', client) //这里只是获取client
    }
}



export default {
    namespaced: true,
    state,
    mutations,
    actions,
}